<template>
    <div class="container addmusic loading">
        <main  class="table-div">
            <el-form ref="ruleForm" :model="form" :rules="rules" label-width="80px" class="demo-ruleForm">
              <el-form-item label="音乐名称" prop="name">
                <el-input v-model="form.name"></el-input>
              </el-form-item>
              <el-form-item label="附件上传">
                <el-upload
                  class="music-uploader"
                  action="#"
                  ref="upload"
                  :limit="1"
                  accept="audio/*"
                  :on-exceed="maxlimit"
                  :on-preview="showtarget"
                  :file-list="fileList"
                  :auto-upload="false">
                    <el-button size="small" type="primary">点击上传</el-button>
                  </el-upload>
              </el-form-item>
              <el-form-item>
                <el-button v-if="!id" type="primary" @click="submitAdd('ruleForm')">确认添加</el-button>
                <el-button v-if="id" type="primary" @click="submitEdit('ruleForm')">确认修改</el-button>
              </el-form-item>
            </el-form>
        </main>
    </div>
</template>

<script>
var chineseLunar = require('chinese-lunar')//eslint-disable-line
import { addMusic, getMusicDetail, editMusic } from '@/api/music'

export default {
  data () {
    return {
      id: '',
      form: {
        name: ''
      },
      fileList: [],
      rules: {
        name: [
          { required: true, message: '请输入音乐名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    maxlimit () {
      this.$message({
        message: '最多添加一首音乐',
        type: 'warning'
      })
    },
    showtarget (e) {
      window.open(e.url)
    },
    getDetails (id) {
      getMusicDetail({ id: id }).then(data => {
        if (data.result === 1) {
          this.form.name = data.data.musicname
          this.fileList.push({ name: data.data.musicpath, url: data.data.musicpath })
        } else {
          this.$message({
            message: data.msg,
            type: 'error'
          })
        }
      })
    },
    submitAdd (formName) {
      // console.log(this.$refs.upload.uploadFiles[0].raw)
      var upfiles = this.$refs.upload.uploadFiles
      if (upfiles.length === 0) {
        this.$message({
          message: '请上传文件',
          type: 'warning'
        })
        return false
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var formData = new FormData()
          Object.entries(this.form).map(item => {
            formData.append(item[0], item[1])
          })
          formData.append('file', upfiles[0].raw)
          addMusic(formData).then((data) => {
            if (data.result === 1) {
              // this.$refs.upload.clearFiles()
              // this.form = {}
              setTimeout(() => {
                this.$router.back()
              }, 1000)
            }
          })
        } else {
          return false
        }
      })
    },
    submitEdit (formName) {
      // console.log(this.$refs.upload.uploadFiles[0].raw)
      var upfiles = this.$refs.upload.uploadFiles
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var formData = new FormData()
          Object.entries(this.form).map(item => {
            formData.append(item[0], item[1])
          })
          formData.append('id', this.id)
          if (upfiles.length !== 0) {
            formData.append('file', upfiles[0].raw)
          }
          editMusic(formData).then((data) => {
            if (data.result === 1) {
              // this.$refs.upload.clearFiles()
              // this.form = {}
              setTimeout(() => {
                this.$router.replace({ name: 'MusicList', params: { page: this.oldPage }})
              }, 1000)
            }
          })
        } else {
          return false
        }
      })
    }
  },
  beforeMount () {
    this.id = this.$route.params.id
    this.oldPage = this.$route.params.page ? this.$route.params.page : ''
    if (this.id) this.getDetails(this.id)
  },
  mounted () {

  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#fff;
$dark_gray:#889aa4;
$light_gray:#eee;
.table-div{
    width:90%;
    margin: 20px auto
}
.title{
    width:90%;
    margin: 10px auto;
    .search-div,.row-div{
      float: left;
    }
    .search-div{
      width: 200px;
      margin-right: 20px
    }
}
.el-upload-list__item{
  height:auto !important;
}
</style>
